
cc.Class({
    extends: cc.Component,

    properties: {
        
        blood:{
            default: null,
            type: cc.Node,
        },
        user:{
            default: null,
            type: cc.Node,
        },
        userBar:{
            default: null,
            type: cc.Node,
        },
        vertigo:{
            default: null,
            type: cc.Node,
        },
        over:{
            default: null,
            type: cc.Node,
        },


        anm:{
                    default: null,
                    type: cc.Animation,
            },

        //士兵速度
        speed: 0,
        
        //攻击范围
        HitRadius: 0,
        
        
        
        
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //监听

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        
        //拿到血量组件
        this.progressBar = this.blood.getComponent(cc.ProgressBar);
        this.progressBar.progress=1;
        this.progressuserBar = this.userBar.getComponent(cc.ProgressBar);
        this.progressuserBar.progress=1;
        this.vertigo.active=false;
        this.overAnm = this.over.getComponent(cc.Animation);

        this.changeState('entrance');

        this.scheduleOnce(function() {
                     // 这里的 this 指向 component
                     this.changeState('run');
                      this.state='run';
                      
                }, 5); 
        //拿到Animation组件
        // this.break = this.node.getComponent(cc.Animation);
            
        
        this.hit = false;
        this.accleft= false;
        this.accel=2.5;
        this.attack=false;
        this.if_behit=false;
        this.hitType=1;

    },

    start () {
        
        
    },
    
    update (dt) {
        //接近
        if(this.state=='run'){
            this.close_user();
            
        }
        
        
        // 每帧判断和主角之间的距离是否在攻击范围
        if (this.UserDistance() < this.HitRadius) {
            this.startAttack(true);
            //是否按下J键
            if(this.hit){
                //被打
                this.beHit(true);
            }
            
        }

        //限制活动范围
        if(this.node.x > 420){
            this.node.x = 420;
        
        }else if (this.node.x < -420) {
            this.node.x = -420;
        };
        
        if(this.node.y > -11.7){
            this.node.y = -11.7;
            
        }else if (this.node.y < -235) {
            this.node.y = -235;
        };
        

    },
    
    onDestroy () {
        // 取消键盘输入监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },
    
    onKeyDown (event) {
        switch(event.keyCode) {
            case cc.macro.KEY.j:
                this.hit=true;
                this.hitType=1;
                break;
                
            case cc.macro.KEY.k:
                this.hit=true;
                this.hitType=2;
                break;
            case cc.macro.KEY.u:
                this.changeState('vertigo');
                this.vertigo.active=true;
                break;    
                
        }
    },
    
    onKeyUp (event) {
        // unset a flag when key released
        switch(event.keyCode) {
            case cc.macro.KEY.j:
                this.hit=false;
                
                break;
            case cc.macro.KEY.k:
                this.hit=false;
                
                break; 
            case cc.macro.KEY.u:
                this.scheduleOnce(function() {
                     // 这里的 this 指向 component
                      this.changeState('run');
                       this.vertigo.active=false;
                }, 1); 
                
                break;       
                
        }
    },
    
    
    //获取主角和士兵的距离
    UserDistance:function () {

        var UserPos = cc.v2(this.user.x, this.user.y + this.user.height/2);
        
 //        // 根据两点位置计算两点之间距离
        var dist = this.node.position.sub(UserPos).mag();
        return dist;
    },
    
    //主角位置
    userPosition:function () {
        var UserPos = this.user.getPosition();
        return UserPos;
    },
    
    

    
    //被攻击
    beHit:function(if_behit){
        //状态控制
        if(this.if_behit == if_behit){
            return;
        }
        this.if_behit = if_behit;



        if(this.if_behit== true){

			if(this.progressBar.progress>0.25){
				this.progressBar.progress-=0.2;
				
				if(this.hitType==1){
				    this.changeState('behit');
				}else {
				    this.changeState('fly');
				}
				
				//被攻击的位置变化
				this.behit_posC(this.hitType);
				//计时器，被打1秒后恢复状态
				this.scheduleOnce(function() {
				         // 这里的 this 指向 component
				          this.changeState('run');
				          this.if_behit=false;
				    }, 1); 
				
			}else{
				this.progressBar.progress=0;
				this.anm.stop();
				this.changeState('break');
				this.scheduleOnce(function() {
				         this.over.active=true;
				         this.overAnm.play("scaleToShow");
				    }, 3); 
			}            


	
            
            

            
            
            
            
               

        }else{
            return;
        }
            
        
    },
    
    
    //自动向主角靠拢
    close_user(){
    	
        var uP = this.user.getPosition();
        var nP = this.node.getPosition();
        if(nP.x<uP.x){
            if(uP.x-nP.x>80){
            this.node.x+=this.speed;
            this.node.scaleX=-1;
            }
            
        }else{
            if(nP.x-uP.x>80){
            this.node.x-=this.speed;
            this.node.scaleX=1;
            }

        };
        if(nP.y<uP.y){
            if(uP.y-nP.y>80){
            this.node.y+=this.speed;
            }
        }else{
            if(nP.y-uP.y>80){
                
            this.node.y-=this.speed;
            }
        };


    },
    
    
    // 血量变化
    blood_beHit:function(){
        if(this.progressBar.progress>0){
            
        
        
        this.progressBar.progress-=0.2;
        }else{
            //播放动画
			
			this.anm.stop();
			
            
        };
    },

    changeState:function(state){
        if (this.state == state){
            return;
        };
        this.state = state;
        if('run'== this.state){
        	this.anm.stop();
            this.anm.play("run");
        }else if ('behit'== this.state){
            this.anm.play("behit");
        }else if ('fly'==this.state){
            this.anm.play("fly");
        }else if('vertigo'==this.state){
        	this.anm.play("vertigo");
        }else if('break'==this.state){
			this.anm.play("break");
		}else if('entrance'==this.state){
			this.anm.play("Mstentrance");
		}
        
        
    },


    behit_posC:function(hitType){
        if(hitType ==1){
            //位置变化
            
           var uP = this.user.getPosition();
            var nP = this.node.getPosition();
            if(nP.x<uP.x){
            //左倒
            cc.tween(this.node)
              .to(0.25, { position: cc.v2(nP.x-50,  nP.y+25)})
              .to(0.5, { position: cc.v2(nP.x-100,  nP.y)})
              
              .start(cc.find('Canvas/cocos'));
                    
            }else{
            //右倒
            cc.tween(this.node)
                .to(0.25, { position: cc.v2(nP.x+50,  nP.y+25)})
              .to(0.5, { position: cc.v2(nP.x+100,  nP.y)})
                
                .start(cc.find('Canvas/cocos'));
            }

        }else if(hitType ==2){
            
           var uP = this.user.getPosition();
            var nP = this.node.getPosition();
            if(nP.x<uP.x){
        //     //左倒
            cc.tween(this.node)
              .to(0.25, { position: cc.v2(nP.x-120,  nP.y+50)})
              .to(0.5, { position: cc.v2(nP.x-150,  nP.y)})
              .to(0.75, { position: cc.v2(nP.x-225,  nP.y+15)})
              .to(1, { position: cc.v2(nP.x-300,  nP.y)})
              .start(cc.find('Canvas/cocos'));
                    
            }else{
            //右倒
            cc.tween(this.node)
                .to(0.25, { position: cc.v2(nP.x+120,  nP.y+15)})
                .to(0.5, { position: cc.v2(nP.x+150,  nP.y+25)})
                .to(0.75, { position: cc.v2(nP.x+225,  nP.y+15)})
                .to(1, { position: cc.v2(nP.x+300,  nP.y)})
                
                .start(cc.find('Canvas/cocos'));
                }
        }
    },
    startAttack:function(attack){
        if(this.attack == attack){
            return ;
        }
        this.attack=attack;

        if(attack==true){
            this.scheduleOnce(function() {
                     // 这里的 this 指向 component
                    this.actionAttack();
                },1);
			this.scheduleOnce(function() {
			         // 这里的 this 指向 component
			        this.startAttack(false);
			    },5);	
        }
        
        
    },

    //
    actionAttack:function(){
        if (this.UserDistance() < this.HitRadius){
            var nP = this.node.getPosition();
            cc.tween(this.user)
              .to(0.15, { position: cc.v2(nP.x-120,  nP.y+50)})
              .to(0.25, { position: cc.v2(nP.x-150,  nP.y)})
              .to(0.35, { position: cc.v2(nP.x-225,  nP.y+15)})
              .to(0.5, { position: cc.v2(nP.x-300,  nP.y)})
              .start(cc.find('Canvas/cocos'));
            this.progressuserBar.progress-=0.1;
        }
    },
    
});
